package com.matt.controller.api.web;

import com.matt.domain.LeadsHistory;
import com.matt.commons.dto.sso.StaffDTO;
import com.matt.foundation.model.StatusResponse;
import com.matt.foundation.query.LeadsHistoryQuery;
import com.matt.service.LeadsHistoryService;
import com.matt.service.LeadsService;
import com.matt.remoteCall.SsoCallService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * ********************
 * description: 线索跟进控制器
 * Created by Ron
 * from com.matt
 * on 2018/6/13 0013.
 */
@RestController
@RequestMapping("/web/leadsHistory")
@Api(tags = "CRM - 线索跟进")
public class LeadsHistoryController {

    @Autowired
    private LeadsHistoryService leadsHistoryService;

    @Autowired
    private LeadsService leadsService;

    @Autowired
    private SsoCallService ssoCallService;

    @RequestMapping(value = "/add" )
    @ApiOperation(value = "增加线索跟进记录", httpMethod = "POST")
    public StatusResponse add(@RequestHeader String ticket, LeadsHistory leadsHistory) {

        StaffDTO staff = ssoCallService.getStaffDTO(ticket);

        String headImgUrl = staff.getHeadImgUrl();

        leadsHistory.setAreaId(staff.getAreaId());
        leadsHistory.setStaffId(staff.getId());
        leadsHistory.setStaffName(staff.getRealName());

        LeadsHistory ret = leadsHistoryService.save(headImgUrl, leadsHistory);
        return StatusResponse.success(ret);
    }

    @RequestMapping(value = "/saveVisit" )
    @ApiOperation(value = "增加访问、试托跟进记录", httpMethod = "POST")
    public StatusResponse saveVisit(@RequestHeader String ticket,@RequestBody LeadsHistory leadsHistory) {

        StaffDTO staff = ssoCallService.getStaffDTO(ticket);

        String headImgUrl = staff.getHeadImgUrl();

        leadsHistory.setAreaId(staff.getAreaId());
        leadsHistory.setStaffId(staff.getId());
        leadsHistory.setStaffName(staff.getRealName());

        LeadsHistory ret = leadsHistoryService.saveVisit(headImgUrl, leadsHistory);
        return StatusResponse.success(ret);
    }


//    @RequestMapping(value = "find_staff")
//    public StatusResponse findStaffInfo(@RequestHeader String ticket) {
//        return StatusResponse.success(ssoCallService.getBaseInfo(ticket));
//    }

    @RequestMapping(value = "/find_by_leadsId")
    @ApiOperation(value = "根据线索id查询", httpMethod = "GET")
    public StatusResponse findByLeadsId(String leadsId, LeadsHistoryQuery leadsHistoryQuery) {
        return StatusResponse.success(leadsHistoryService.findByLeadsId(leadsId, leadsHistoryQuery));
    }

    @RequestMapping(value = "/findByVisitId")
    @ApiOperation(value = "根据访问id查询", httpMethod = "GET")
    public StatusResponse findByVisitId(String leadsId, LeadsHistoryQuery leadsHistoryQuery) {
        return StatusResponse.success(leadsHistoryService.findByVisitId(leadsId, leadsHistoryQuery));
    }

    @RequestMapping(value = "/findByPalletId")
    @ApiOperation(value = "根据试托id查询", httpMethod = "GET")
    public StatusResponse findByPalletId(String leadsId, LeadsHistoryQuery leadsHistoryQuery) {
        return StatusResponse.success(leadsHistoryService.findByPalletId(leadsId, leadsHistoryQuery))   ;
    }



    @RequestMapping(value = "/find_by_clientelesId")
    @ApiOperation(value = "根据客户id查询", httpMethod = "POST")
    public StatusResponse findByClienteleId(String clientelesId, LeadsHistoryQuery leadsHistoryQuery) {
        // 1.只查客户创建的线索
        //return StatusResponse.success(leadsHistoryService.findByClienteleId(ticket,clientelesId,leadsHistoryQuery));
        // 2.查询线索+客户创建的销售动态
        return StatusResponse.success(leadsHistoryService.findAll(clientelesId, leadsHistoryQuery));
    }


}
